package com.zhengke.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhengke.common.util.LoadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.common.object.Result;
import com.zhengke.dao.tvindex.HomePicMapper;
import com.zhengke.service.IHomePic;

@Service
public class HomePicImpl implements IHomePic {

	@Autowired
	private HomePicMapper dao;
	
	@Override
	public Result findHomePic(String name, int limit, int page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		PageHelper.startPage(page,limit,true);
		List<Map<String , Object>> rtnList = dao.findHomePic(map);
		//读取config.properties查找访问路径的ip+端口
		String fileServerUrl = null;
		try {
			fileServerUrl = LoadProperties.findValue("FILE_SERVER_URL");
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (Map<String,Object> map1 : rtnList){
			String pic_path = (String) map1.get("pic_path");
			pic_path = fileServerUrl + pic_path;
			map1.replace("pic_path",pic_path);
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(rtnList);
		Result rs = new Result();
		rs.setCode("0");
		rs.setCount(pageInfo.getTotal());
		rs.setData(rtnList);
		rs.setMsg("success");
		return rs;
	}

	@Override
	public int updateHomepic(String pic_path, String name,int id) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pic_path", pic_path);
		map.put("name", name);
		map.put("id",id);
		int rs=dao.updateHomepic(map);
		return rs;
	}

}
